<template>
  <view class="lost-found-page">
    <!-- 搜索栏 -->
    <view class="search-section">
      <u-search 
        v-model="searchKeyword" 
        placeholder="搜索失物信息"
        :show-action="false"
        bg-color="#f5f5f5"
        @search="handleSearch"
        @clear="handleClear"
      ></u-search>
    </view>

    <!-- 分类筛选 -->
    <view class="filter-section">
      <scroll-view scroll-x enable-flex class="filter-scroll">
        <view class="filter-list">
          <view 
            v-for="(item, index) in filterList" 
            :key="index"
            class="filter-item"
            :class="{ active: currentFilter === item.value }"
            @click="handleFilterChange(item.value)"
          >
            <text class="filter-text">{{ item.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 失物招领列表 -->
    <view class="lost-found-list">
      <view 
        v-for="(item, index) in lostFoundList" 
        :key="item.id"
        class="lost-found-item"
        @click="handleItemClick(item)"
      >
        <!-- 失物类型标签 -->
        <view class="type-badge" :class="getTypeClass(item.type)">
          <text class="type-text">{{ item.typeText }}</text>
        </view>

        <!-- 失物图片 -->
        <view class="item-image">
          <image :src="item.image" mode="aspectFill" class="image"></image>
        </view>

        <!-- 失物内容 -->
        <view class="item-content">
          <view class="content-header">
            <text class="item-title">{{ item.title }}</text>
            <view class="item-status">
              <u-tag 
                :text="item.statusText" 
                :type="getStatusType(item.status)"
                size="mini"
              ></u-tag>
            </view>
          </view>
          
          <view class="content-body">
            <text class="item-description">{{ item.description }}</text>
          </view>
          
          <view class="content-footer">
            <view class="item-info">
              <view class="info-item">
                <u-icon name="user" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.publisher }}</text>
              </view>
              <view class="info-item">
                <u-icon name="map-pin" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.location }}</text>
              </view>
              <view class="info-item">
                <u-icon name="clock" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.publishTime }}</text>
              </view>
            </view>
          </view>
          
          <view class="reward-button-row">
            <view class="item-reward">
              <text class="reward-text" :class="{ free: item.reward === 0 }">
                {{ item.reward === 0 ? '免费' : `¥${item.reward}` }}
              </text>
            </view>
            
            <!-- 操作按钮 -->
            <view class="action-btn" :class="getButtonClass(item.status)" @click.stop="handleAction(item)">
              <text class="btn-text">{{ getButtonText(item.status) }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="lostFoundList.length === 0" class="empty-state">
      <u-icon name="search" size="48" color="#ccc"></u-icon>
      <text class="empty-text">暂无失物招领信息</text>
    </view>

    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more">
      <u-loading-icon v-if="loading" mode="circle"></u-loading-icon>
      <text v-else class="load-text" @click="loadMore">点击加载更多</text>
    </view>

    <!-- 发布按钮 -->
    <view class="publish-btn" @click="handlePublish">
      <u-icon name="plus" size="20" color="#fff"></u-icon>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

// 搜索关键词
const searchKeyword = ref('')

// 当前筛选条件
const currentFilter = ref('all')

// 筛选列表
const filterList = reactive([
  { label: '全部', value: 'all' },
  { label: '丢失', value: 'lost' },
  { label: '拾到', value: 'found' },
  { label: '进行中', value: 'ongoing' },
  { label: '已认领', value: 'claimed' }
])

// 失物招领列表数据
const lostFoundList = ref([
  {
    id: 1,
    title: '丢失黑色钱包',
    description: '在小区门口丢失黑色钱包，内有身份证、银行卡等重要证件，如有拾到请联系...',
    type: 'lost',
    typeText: '丢失',
    status: 1,
    statusText: '寻找中',
    publisher: '张先生',
    location: '小区门口',
    publishTime: '2小时前',
    reward: 200,
    image: 'https://example.com/images/wallet.jpg'
  },
  {
    id: 2,
    title: '拾到红色手机',
    description: '在小区花园拾到一部红色手机，手机壳是卡通图案，请失主联系认领...',
    type: 'found',
    typeText: '拾到',
    status: 1,
    statusText: '待认领',
    publisher: '李女士',
    location: '小区花园',
    publishTime: '1天前',
    reward: 0,
    image: 'https://example.com/images/phone.jpg'
  },
  {
    id: 3,
    title: '丢失蓝色背包',
    description: '在小区停车场丢失蓝色背包，内有笔记本电脑和重要文件，急寻...',
    type: 'lost',
    typeText: '丢失',
    status: 2,
    statusText: '已找到',
    publisher: '王女士',
    location: '小区停车场',
    publishTime: '3天前',
    reward: 500,
    image: 'https://example.com/images/backpack.jpg'
  },
  {
    id: 4,
    title: '拾到白色耳机',
    description: '在小区电梯内拾到白色无线耳机，请失主联系认领，免费归还...',
    type: 'found',
    typeText: '拾到',
    status: 3,
    statusText: '已认领',
    publisher: '刘先生',
    location: '小区电梯',
    publishTime: '5天前',
    reward: 0,
    image: 'https://example.com/images/earphones.jpg'
  },
  {
    id: 5,
    title: '丢失金色项链',
    description: '在小区广场丢失金色项链，有特殊纪念意义，如有拾到请联系，重金酬谢...',
    type: 'lost',
    typeText: '丢失',
    status: 1,
    statusText: '寻找中',
    publisher: '陈女士',
    location: '小区广场',
    publishTime: '1周前',
    reward: 1000,
    image: 'https://example.com/images/necklace.jpg'
  }
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 获取类型样式类
const getTypeClass = (type: string) => {
  switch (type) {
    case 'lost': return 'type-lost'
    case 'found': return 'type-found'
    default: return 'type-default'
  }
}

// 获取状态类型
const getStatusType = (status: number) => {
  switch (status) {
    case 1: return 'primary'
    case 2: return 'success'
    case 3: return 'info'
    default: return 'info'
  }
}

// 获取按钮样式类
const getButtonClass = (status: number) => {
  switch (status) {
    case 1: return 'btn-claim'
    case 2: return 'btn-found'
    case 3: return 'btn-claimed'
    default: return 'btn-default'
  }
}

// 获取按钮文本
const getButtonText = (status: number) => {
  switch (status) {
    case 1: return '联系认领'
    case 2: return '已找到'
    case 3: return '已认领'
    default: return '查看详情'
  }
}

// 搜索处理
const handleSearch = (value: string) => {
  console.log('搜索失物信息:', value)
  // 这里实现搜索逻辑
}

// 清空搜索
const handleClear = () => {
  searchKeyword.value = ''
  console.log('清空搜索')
}

// 筛选条件改变
const handleFilterChange = (value: string) => {
  currentFilter.value = value
  console.log('筛选条件改变:', value)
  // 这里实现筛选逻辑
}

// 点击失物项
const handleItemClick = (item: any) => {
  console.log('点击失物信息:', item)
  // 跳转到失物详情页面
  uni.navigateTo({
    url: `/pages/lost-and-found/detail?id=${item.id}`
  })
}

// 操作处理
const handleAction = (item: any) => {
  if (item.status === 1) {
    // 跳转到联系认领页面
    uni.navigateTo({
      url: `/pages/lost-and-found/claim?id=${item.id}`
    })
  } else if (item.status === 2) {
    uni.showToast({
      title: '物品已找到',
      icon: 'none'
    })
  } else if (item.status === 3) {
    uni.showToast({
      title: '物品已认领',
      icon: 'none'
    })
  }
}

// 发布失物招领
const handlePublish = () => {
  uni.navigateTo({
    url: '/pages/lost-and-found/publish'
  })
}

// 加载更多
const loadMore = () => {
  if (loading.value) return
  
  loading.value = true
  setTimeout(() => {
    // 模拟加载更多数据
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  console.log('失物招领页面加载完成')
})
</script>

<style lang="scss" scoped>
.lost-found-page {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 0 0 20rpx 0;
}

.page-header {
  background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
  padding: 20rpx 30rpx 30rpx;
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .header-title {
      .title-text {
        display: block;
        font-size: 36rpx;
        font-weight: 600;
        color: #fff;
        margin-bottom: 8rpx;
      }
      
      .title-subtitle {
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.8);
      }
    }
    
    .header-icon {
      width: 80rpx;
      height: 80rpx;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.search-section {
  padding: 24rpx 32rpx;
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.filter-section {
  background-color: #fff;
  border-bottom: 1rpx solid #f0f0f0;
  
  .filter-scroll {
    white-space: nowrap;
    padding: 0 24rpx 24rpx 24rpx;
    
    .filter-list {
      display: flex;
      padding: 24rpx 0 0 0;
      width: max-content;
      
      .filter-item {
        flex-shrink: 0;
        padding: 16rpx 20rpx;
        margin-right: 12rpx;
        border-radius: 30rpx;
        background-color: #f5f5f5;
        transition: all 0.3s ease;
        
        &.active {
          background-color: #52c41a;
          
          .filter-text {
            color: #fff;
          }
        }
        
        .filter-text {
          font-size: 24rpx;
          color: #666;
          white-space: nowrap;
        }
      }
    }
  }
}

.lost-found-list {
  padding: 20rpx 30rpx;
  
  .lost-found-item {
    background-color: #fff;
    border-radius: 16rpx;
    margin-bottom: 20rpx;
    padding: 30rpx;
    position: relative;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
    
    &:active {
      transform: scale(0.98);
    }
    
    .type-badge {
      position: absolute;
      top: 20rpx;
      right: 20rpx;
      padding: 8rpx 16rpx;
      border-radius: 20rpx;
      font-size: 20rpx;
      
      &.type-lost {
        background-color: #ff4d4f;
        color: #fff;
      }
      
      &.type-found {
        background-color: #52c41a;
        color: #fff;
      }
    }
    
    .item-image {
      width: 100%;
      height: 300rpx;
      border-radius: 12rpx;
      overflow: hidden;
      margin-bottom: 20rpx;
      
      .image {
        width: 100%;
        height: 100%;
      }
    }
    
    .item-content {
      .content-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 16rpx;
        
        .item-title {
          flex: 1;
          font-size: 32rpx;
          font-weight: 600;
          color: #333;
          line-height: 1.4;
          margin-right: 20rpx;
        }
        
        .item-status {
          flex-shrink: 0;
        }
      }
      
      .content-body {
        margin-bottom: 20rpx;
        
        .item-description {
          font-size: 28rpx;
          color: #666;
          line-height: 1.5;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
      
      .content-footer {
        margin-bottom: 20rpx;
        
        .item-info {
          display: flex;
          flex-direction: column;
          gap: 12rpx;
          
          .info-item {
            display: flex;
            align-items: center;
            gap: 8rpx;
            
            .info-text {
              font-size: 24rpx;
              color: #999;
            }
          }
        }
      }
      
      .reward-button-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 20rpx;
        
        .item-reward {
          flex: 1;
          
          .reward-text {
            font-size: 28rpx;
            font-weight: 600;
            color: #ff4d4f;
            
            &.free {
              color: #52c41a;
            }
          }
        }
        
        .action-btn {
          flex-shrink: 0;
          padding: 16rpx 32rpx;
          border-radius: 40rpx;
          font-size: 24rpx;
          font-weight: 500;
          transition: all 0.3s ease;
          
          &.btn-claim {
            background-color: #52c41a;
            color: #fff;
            
            &:active {
              background-color: #389e0d;
            }
          }
          
          &.btn-found {
            background-color: #1890ff;
            color: #fff;
          }
          
          &.btn-claimed {
            background-color: #d9d9d9;
            color: #666;
          }
          
          .btn-text {
            font-size: 24rpx;
          }
        }
      }
    }
    
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-text {
    margin-top: 20rpx;
    font-size: 28rpx;
    color: #999;
  }
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx 0;
  
  .load-text {
    font-size: 28rpx;
    color: #52c41a;
  }
}

.publish-btn {
  position: fixed;
  bottom: 120rpx;
  right: 30rpx;
  width: 100rpx;
  height: 100rpx;
  background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 12rpx rgba(82, 196, 26, 0.3);
  transition: all 0.3s ease;
  
  &:active {
    transform: scale(0.95);
  }
}
</style>
